//
//  LoadingView.m
//  PayManager
//
//  Created by app on 2017/6/21.
//  Copyright © 2017年 HZJiTeng. All rights reserved.
//

#import "LoadingView.h"

@interface LoadingView ()
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,assign)NSInteger timeIndex;
@property (nonatomic,assign)BOOL isLoading;
@property (nonatomic,strong)UIView *superView;

@end

@implementation LoadingView

- (instancetype)initInView:(UIView *)view withTips:(NSString *)tips withTimeDelay:(CGFloat)timeDelay {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(0, STATUS_BAR_HEIGHT+44, SCREEN_WIDTH, SCREEN_HEIGHT-STATUS_BAR_HEIGHT-44);
        [view addSubview:self];
        _timeDelay = timeDelay;
        _tips = tips;
        [self createUI];
        _superView = view;
    }
    return self;
}

- (void)createUI {
    UIView *backView = [[UIView alloc]init];
    backView.layer.cornerRadius = 8;
    backView.clipsToBounds = YES;
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.6;
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self).offset(-32);
        make.centerY.equalTo(self).offset(-85);
        make.centerX.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(140, 130));
    }];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    NSMutableArray *imageArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 11; i++) {
        UIImage *imageName = [UIImage imageNamed:[NSString stringWithFormat:@"loading%ld",i]];
        [imageArray addObject:imageName];
    }
    imageView.image = IMAGE_NAME(@"loading0");
    imageView.animationImages = imageArray;
    imageView.animationRepeatCount = MAXFLOAT;
    imageView.animationDuration = 1.1f;
    [self addSubview:imageView];
    _imageView = imageView;
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backView).offset(-10);
        make.centerX.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    UILabel *tipsLabel = [[UILabel alloc]init];
    tipsLabel.text = @"loading……";
    if (!IsStrEmpty(_tips)) {
        tipsLabel.text = _tips;
    }
    tipsLabel.textColor = [UIColor whiteColor];
    tipsLabel.textAlignment = NSTextAlignmentCenter;
    tipsLabel.font = [UIFont systemFontOfSize:14];
    tipsLabel.numberOfLines = 0;
    [backView addSubview:tipsLabel];
    _tipsLabel = tipsLabel;
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(8);
        make.left.mas_equalTo(8);
        make.width.mas_equalTo(140 -16);
        make.height.mas_equalTo(35);
    }];
    
}

- (void)startLoading {
    [_superView bringSubviewToFront:self];
    if (!_isLoading) {
        [_imageView startAnimating];
        _isLoading = YES;
        [self performSelector:@selector(endLoading) withObject:nil afterDelay:_timeDelay];
    }
}
- (void)endLoading {
    _isLoading = NO;
    [_imageView stopAnimating];
    if (self.delegate && [self.delegate respondsToSelector:@selector(endLoading)]) {
        [self.delegate endLoading];
    }
}
- (void)setTips:(NSString *)tips {
    _tips = tips;
    _tipsLabel.text = tips;
}

+ (LoadingView *)shareManagerInView:(UIView *)view withTips:(NSString *)tips withTimeDelay:(CGFloat)timeDelay {
    return [[LoadingView alloc]initInView:view withTips:tips withTimeDelay:timeDelay];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
